package org.yelongframework.video.m3u8;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yelongframework.commons.io.FileUtil;
import org.yelongframework.commons.lang.IntegerUtil;
import org.yelongframework.commons.lang.NumberUtil;
import org.yelongframework.commons.lang.RuntimeUtil;
import org.yelongframework.core.lang.process.ProcessEntry;
import org.yelongframework.core.lang.process.SimpleProcessEntry;

/**
 * 标准实现
 * 
 * @since 1.0.0
 */
public class StandardM3u8Integrator implements M3u8Integrator {

	private static final Logger logger = LoggerFactory.getLogger(StandardM3u8Integrator.class);

	@Override
	public void integration(File directory, String destFilePath, boolean override) throws IOException {
		if (!directory.exists()) {
			throw new IOException(directory + " not exists");
		}
		if (!directory.isDirectory()) {
			throw new IOException(directory + " not a directory");
		}
		if (override) {
			FileUtil.deleteQuietly(FileUtil.getFile(destFilePath));
		} else {
			if (FileUtil.exists(destFilePath)) {
				throw new IOException(destFilePath + " exists");
			}
		}
		// 仅获取名称最后一个是数字的文件
		File[] fileArray = directory.listFiles((d, f) -> {
			String name = FilenameUtils.getBaseName(f);
			char lastChar = name.charAt(name.length() - 1);
			return NumberUtil.isNumber(lastChar);
		});
		List<File> files = Stream.of(fileArray).sorted((x, y) -> {
			String Xname = x.getName();
			String XfileName = FilenameUtils.getBaseName(Xname);
			String Yname = y.getName();
			String YfileName = FilenameUtils.getBaseName(Yname);
			Integer Xint = IntegerUtil.valueOfByEnd(XfileName, 0);
			Integer Yint = IntegerUtil.valueOfByEnd(YfileName, 0);
			return Integer.compare(Xint, Yint);
		}).collect(Collectors.toList());
		String cmd = generateCMD(files, destFilePath, false);
		logger.info(cmd);
		Process process = RuntimeUtil.exec(cmd, directory);
		ProcessEntry processEntry = new SimpleProcessEntry(process, "GBK");
		if (processEntry.isError()) {
			throw new IOException(processEntry.getErrorStreamString());
		}
	}

	protected String generateCMD(List<File> files, String destFilePath, boolean fullPath) {
		StringBuilder cmd = new StringBuilder("cmd /c copy /b ");
		String fileCmd = files.stream().map(x -> {
			if (fullPath) {
				return x.getAbsolutePath();
			} else {
				return x.getName();
			}
		}).collect(Collectors.joining("+"));
		cmd.append(fileCmd);
		cmd.append(" ");
		cmd.append(destFilePath);
		return cmd.toString();
	}

	@Override
	public void integration(List<File> files, String destFilePath, boolean override) throws IOException {
		if (override) {
			FileUtil.deleteQuietly(destFilePath);
		} else {
			if (FileUtil.exists(destFilePath)) {
				throw new IOException(destFilePath + " exists");
			}
		}
		String cmd = generateCMD(files, destFilePath, true);
		logger.info(cmd);
		Process process = RuntimeUtil.exec(cmd);
		ProcessEntry processEntry = new SimpleProcessEntry(process, "GBK");
		if (processEntry.isError()) {
			throw new IOException(processEntry.getErrorStreamString());
		}
	}

}
